/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
module dwt.custom.ccombo;


private import dwt.dwt;

private import dwt.graphics.color;
private import dwt.graphics.font;
private import dwt.graphics.point;
private import dwt.graphics.rectangle;
private import dwt.events.events;
private import dwt.events.listeners;
private import dwt.widgets.button;
private import dwt.widgets.composite;
private import dwt.widgets.control;
private import dwt.widgets.display;
private import dwt.widgets.event;
private import dwt.widgets.label;
private import dwt.widgets.list;
private import dwt.widgets.listener;
private import dwt.widgets.shell;
private import dwt.widgets.text;
private import dwt.widgets.typedlistener;

version(OLE_COM){
private import dwt.accessibility.acc;
private import dwt.accessibility.accessible;
private import dwt.accessibility.accevents;
}

private import dwt.util.util;
private import dwt.util.eventhandler;
private import dwt.internal.converter;

/**
 * The CCombo class represents a selectable user interface object
 * that combines a text field and a list and issues notificiation
 * when an item is selected from the list.
 * <p>
 * Note that although this class is a subclass of <code>Composite</code>,
 * it does not make sense to add children to it, or set a layout on it.
 * </p>
 * <dl>
 * <dt><b>Styles:</b>
 * <dd>BORDER, READ_ONLY, FLAT</dd>
 * <dt><b>Events:</b>
 * <dd>Selection</dd>
 * </dl>
 */
public class CCombo : Composite {

	Text text;
	List list;
	int visibleItemCount = 5;
	Shell popup;
	Button arrow;
	boolean hasFocus;
	Listener listener, filter;
	Color foreground, background;
	Font font;
	
/**
 * Constructs a new instance of this class given its parent
 * and a style value describing its behavior and appearance.
 * <p>
 * The style value is either one of the style constants defined in
 * class <code>DWT</code> which is applicable to instances of this
 * class, or must be built by <em>bitwise OR</em>'ing together 
 * (that is, using the <code>int</code> "|" operator) two or more
 * of those <code>DWT</code> style constants. The class description
 * lists the style constants that are applicable to the class.
 * Style bits are also inherited from superclasses.
 * </p>
 *
 * @param parent a widget which will be the parent of the new instance (cannot be null)
 * @param style the style of widget to construct
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
 * </ul>
 *
 * @see DWT#BORDER
 * @see DWT#READ_ONLY
 * @see DWT#FLAT
 * @see Widget#getStyle()
 */
public this (Composite parent, int style) {
	super (parent, style = checkStyle (style));
	
	int textStyle = DWT.SINGLE;
	if ((style & DWT.READ_ONLY) != 0) textStyle |= DWT.READ_ONLY;
	if ((style & DWT.FLAT) != 0) textStyle |= DWT.FLAT;
	text = new Text (this, textStyle);
	int arrowStyle = DWT.ARROW | DWT.DOWN;
	if ((style & DWT.FLAT) != 0) arrowStyle |= DWT.FLAT;
	arrow = new Button (this, arrowStyle);

	class LS1 : Listener {
		CCombo c;
		public this(CCombo cc) {c = cc;}
		public void handleEvent (Event event) {
			if (c.popup is event.widget) {
				c.popupEvent (event);
				return;
			}
			if (c.text is event.widget) {
				c.textEvent (event);
				return;
			}
			if (c.list is event.widget) {
				c.listEvent (event);
				return;
			}
			if (c.arrow is event.widget) {
				c.arrowEvent (event);
				return;
			}
			if (c is event.widget) {
				c.comboEvent (event);
				return;
			}
			if (c.getShell () is event.widget) {
				c.handleFocus (DWT.FocusOut);
			}
		}
	}
	listener = new LS1 (this) ;
	class LS2 : Listener {
		CCombo c;
		public this(CCombo cc) {c = cc;}
		public void handleEvent(Event event) {
			Shell shell = (cast(Control)event.widget).getShell ();
			if (c.getShell() is shell) {
				c.handleFocus (DWT.FocusOut);
			}
		}
	}
	filter = new LS2(this) ;
	
	int [] comboEvents;
	comboEvents ~= DWT.Dispose;
	comboEvents ~= DWT.Move;
	comboEvents ~= DWT.Resize;
	for (int i=0; i<comboEvents.length; i++) 
		this.addListener (comboEvents [i], listener);
	
	int [] textEvents;
	textEvents ~= DWT.KeyDown;
	textEvents ~= DWT.KeyUp;
	textEvents ~= DWT.Modify;
	textEvents ~= DWT.MouseDown;
	textEvents ~= DWT.MouseUp;
	textEvents ~= DWT.Traverse;
	textEvents ~= DWT.FocusIn;
	for (int i=0; i<textEvents.length; i++) 
		text.addListener (textEvents [i], listener);
	
	int [] arrowEvents;
	arrowEvents ~= DWT.Selection;
	arrowEvents ~= DWT.FocusIn;
	for (int i=0; i<arrowEvents.length; i++) arrow.addListener (arrowEvents [i], listener);
	
	createPopup(null, -1);	
	version(OLE_COM){
	initAccessible();
	}
}
static int checkStyle (int style) {
	int mask = DWT.BORDER | DWT.READ_ONLY | DWT.FLAT | DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT;
	return style & mask;
}
/**
* Adds an item.
* <p>
* The item is placed at the end of the list.
* Indexing is zero based.
*
* @param string the new item
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when the string is null
* @exception SWTError(ERROR_ITEM_NOT_ADDED)
*	when the item cannot be added
*/
public void add (char[] string) {
	checkWidget();
	if (string is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	list.add (string);
}
/**
* Adds an item at an index.
* <p>
* The item is placed at an index in the list.
* Indexing is zero based.
*
* This operation will fail when the index is
* out of range.
*
* @param string the new item
* @param index the index for the item
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when the string is null
* @exception SWTError(ERROR_ITEM_NOT_ADDED)
*	when the item cannot be added
*/
public void add (char[] string, int index) {
	checkWidget();
	if (string is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	list.add (string, index);
}
/**	 
* Adds the listener to receive events.
* <p>
*
* @param listener the listener
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when listener is null
*/
public void addModifyListener (ModifyListener listener) {
	checkWidget();
	if (listener is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	TypedListener typedListener = new TypedListener (listener);
	addListener (DWT.Modify, typedListener);
}
/**	 
* Adds the listener to receive events.
* <p>
*
* @param listener the listener
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when listener is null
*/
public void addSelectionListener(SelectionListener listener) {
	checkWidget();
	if (listener is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	TypedListener typedListener = new TypedListener (listener);
	addListener (DWT.Selection,typedListener);
	addListener (DWT.DefaultSelection,typedListener);
}
void arrowEvent (Event event) {
	switch (event.type) {
		case DWT.FocusIn: {
			handleFocus (DWT.FocusIn);
			break;
		}
		case DWT.Selection: {
			dropDown (cast(boolean)!isDropped ());
			break;
		}
		default : break;
	}
}
/**
* Clears the current selection.
* <p>
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public void clearSelection () {
	checkWidget();
	text.clearSelection ();
	list.deselectAll ();
}
void comboEvent (Event event) {
	switch (event.type) {
		case DWT.Dispose:
			if (popup !is null && !popup.isDisposed ()) {
				list.removeListener(DWT.Dispose, listener);
				popup.dispose ();
			}
			Shell shell = getShell ();
			shell.removeListener(DWT.Deactivate, listener);
			Display display = getDisplay();
			display.removeFilter(DWT.FocusIn, filter);
			popup = null;  
			text = null;  
			list = null;  
			arrow = null;
			break;
		case DWT.Move:
			dropDown(false);
			break;
		case DWT.Resize:
			internalLayout();
			break;
		default : break;
	}
}

public Point computeSize (int wHint, int hHint, boolean changed) {
	checkWidget();
	int width = 0, height = 0;
	Point textSize = text.computeSize (wHint, DWT.DEFAULT, changed);
	Point arrowSize = arrow.computeSize(DWT.DEFAULT, DWT.DEFAULT, changed);
	Point listSize = list.computeSize (wHint, DWT.DEFAULT, changed);
	int borderWidth = getBorderWidth();
	
	height = Math.max (hHint, Math.max(textSize.y, arrowSize.y)  + 2*borderWidth);
	width = Math.max (wHint, Math.max(textSize.x + arrowSize.x + 2*borderWidth, listSize.x + 2)  );
	return new Point (width, height);
}
void createPopup(char[][] items, int selectionIndex) {		
	// create shell and list
	popup = new Shell (getShell(), DWT.NO_TRIM | DWT.ON_TOP);
	int style = getStyle();
	int listStyle = DWT.SINGLE | DWT.V_SCROLL;
	if ((style & DWT.FLAT) != 0) listStyle |= DWT.FLAT;
	if ((style & DWT.RIGHT_TO_LEFT) != 0) listStyle |= DWT.RIGHT_TO_LEFT;
	if ((style & DWT.LEFT_TO_RIGHT) != 0) listStyle |= DWT.LEFT_TO_RIGHT;
	list = new List (popup, listStyle);
	if (font !is null) list.setFont(font);
	if (foreground !is null) list.setForeground(foreground);
	if (background !is null) list.setBackground(background);
	
	int [] popupEvents;
	popupEvents ~= DWT.Close;
	popupEvents ~= DWT.Paint;
	popupEvents ~= DWT.Deactivate;
	for (int i=0; i<popupEvents.length; i++) popup.addListener (popupEvents [i], listener);
	int [] listEvents;
	listEvents ~= DWT.MouseUp;
	listEvents ~= DWT.Selection;
	listEvents ~= DWT.Traverse;
	listEvents ~= DWT.KeyDown;
	listEvents ~= DWT.KeyUp;
	listEvents ~= DWT.FocusIn;
	listEvents ~= DWT.Dispose;
	for (int i=0; i<listEvents.length; i++) list.addListener (listEvents [i], listener);
	
	if (items !is null) list.setItems(items);
	if (selectionIndex != -1) list.setSelection(selectionIndex);
}
/**
* Deselects an item.
* <p>
* If the item at an index is selected, it is
* deselected.  If the item at an index is not
* selected, it remains deselected.  Indices
* that are out of range are ignored.  Indexing
* is zero based.
*
* @param index the index of the item
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public void deselect (int index) {
	checkWidget();
	list.deselect (index);
}
/**
* Deselects all items.
* <p>
*
* If an item is selected, it is deselected.
* If an item is not selected, it remains unselected.
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public void deselectAll () {
	checkWidget();
	list.deselectAll ();
}
void dropDown (boolean drop) {
	if (drop == isDropped ()) return;
	if (!drop) {
		popup.setVisible (false);
		text.setFocus();
		return;
	}

	if (getShell() !is popup.getParent()) {
		char[][] items = list.getItems();
		int selectionIndex = list.getSelectionIndex();
		list.removeListener(DWT.Dispose, listener);
		popup.dispose();
		popup = null;
		list = null;
		createPopup(items, selectionIndex);
	}
	
	Point size = getSize();
	int itemCount = list.getItemCount();
	itemCount = (itemCount == 0) ? visibleItemCount : Math.min(visibleItemCount, itemCount);
	int itemHeight = list.getItemHeight () * itemCount;
	Point listSize = (cast(Control)list).computeSize (DWT.DEFAULT, itemHeight);
	(cast(Control)list).setBounds (1, 1, Math.max (size.x - 2, listSize.x), listSize.y);
	
	int index = list.getSelectionIndex ();
	if (index != -1) list.setTopIndex (index);
	Display display = getDisplay ();
	Rectangle listRect = list.getBounds ();
	Rectangle parentRect = display.map (getParent (), null, getBounds());
	Point comboSize = getSize ();
	Rectangle displayRect = getMonitor().getClientArea();
	int width = Math.max (comboSize.x, listRect.width + 2);
	int height = listRect.height + 2;
	int x = parentRect.x;
	int y = parentRect.y + comboSize.y;
	if (y + height > displayRect.y + displayRect.height) y = parentRect.y - height;
	(cast(Control)popup).setBounds (x, y, width, height);
	popup.setVisible (true);
	list.setFocus();
}
/* 
 * Return the Label immediately preceding the receiver in the z-order, 
 * or null if none. 
 */
Label getAssociatedLabel () {
	Control[] siblings = getParent().getChildren();
	for (int i = 0; i < siblings.length; i++) {
		if (siblings[i] is this) {
			if (i > 0 && cast(Label)siblings[i-1] ) {
				return cast(Label)siblings[i-1];
			}
		}
	}
	return null;
}
public Control [] getChildren () {
	checkWidget();
	return new Control [0];
}
/**
 * Gets the editable state.
 * 
 * @return true if the contents can be edited
 *
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 *
 * @since 3.0
 *
 */
public boolean getEditable () {
	checkWidget ();
	return text.getEditable();
}
/**
* Gets an item at an index.
* <p>
* Indexing is zero based.
*
* This operation will fail when the index is out
* of range or an item could not be queried from
* the OS.
*
* @param index the index of the item
* @return the item
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_CANNOT_GET_ITEM)
*	when the operation fails
*/
public char[] getItem (int index) {
	checkWidget();
	return list.getItem (index);
}
/**
* Gets the number of items.
* <p>
* This operation will fail if the number of
* items could not be queried from the OS.
*
* @return the number of items in the widget
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_CANNOT_GET_COUNT)
*	when the operation fails
*/
public int getItemCount () {
	checkWidget();
	return list.getItemCount ();
}
/**
* Gets the height of one item.
* <p>
* This operation will fail if the height of
* one item could not be queried from the OS.
*
* @return the height of one item in the widget
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_CANNOT_GET_ITEM_HEIGHT)
*	when the operation fails
*/
public int getItemHeight () {
	checkWidget();
	return list.getItemHeight ();
}
/**
* Gets the items.
* <p>
* This operation will fail if the items cannot
* be queried from the OS.
*
* @return the items in the widget
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_CANNOT_GET_ITEM)
*	when the operation fails
*/
public char[][] getItems () {
	checkWidget();
	return list.getItems ();
}
char getMnemonic (char[] string) {
	int index = 0;
	int length = string.length;
	do {
		while ((index < length) && (string[index] != '&')) index++;
		if (++index >= length) return '\0';
		if (string[index] != '&') return string[index];
		index++;
	} while (index < length);
 	return '\0';
}
/**
* Gets the selection.
* <p>
* @return a point representing the selection start and end
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public Point getSelection () {
	checkWidget();
	return text.getSelection ();
}
/**
* Gets the index of the selected item.
* <p>
* Indexing is zero based.
* If no item is selected -1 is returned.
*
* @return the index of the selected item.
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public int getSelectionIndex () {
	checkWidget();
	return list.getSelectionIndex ();
}
public int getStyle () {
	int style = super.getStyle();
	style &= ~DWT.READ_ONLY;
	if (!text.getEditable()) style |= DWT.READ_ONLY; 
	return style;
}
/**
* Gets the widget text.
* <p>
* If the widget has no text, an empty string is returned.
*
* @return the widget text
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public char[] getText () {
	checkWidget();
	return text.getText ();
}
/**
* Gets the height of the combo's text field.
* <p>
* The operation will fail if the height cannot 
* be queried from the OS.

* @return the height of the combo's text field.
* 
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_ERROR_CANNOT_GET_ITEM_HEIGHT)
*	when the operation fails
*/
public int getTextHeight () {
	checkWidget();
	return text.getLineHeight();
}
/**
* Gets the text limit.
* <p>
* @return the text limit
* 
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public int getTextLimit () {
	checkWidget();
	return text.getTextLimit ();
}
/**
 * Gets the number of items that are visible in the drop
 * down portion of the receiver's list.
 *
 * @return the number of items that are visible
 *
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 * 
 * @since 3.0
 */
public int getVisibleItemCount () {
	checkWidget ();
	return visibleItemCount;
}
void handleFocus (int type) {
	if (isDisposed()) return;
	switch (type) {
		case DWT.FocusIn: {
			if (hasFocus) return;
			if (getEditable ()) text.selectAll ();
			hasFocus = true;
			Shell shell = getShell ();
			shell.removeListener(DWT.Deactivate, listener);
			shell.addListener(DWT.Deactivate, listener);
			Display display = getDisplay();
			display.removeFilter(DWT.FocusIn, filter);
			display.addFilter(DWT.FocusIn, filter);
			Event e = new Event();
			notifyListeners(DWT.FocusIn, e);
			break;
		}
		case DWT.FocusOut: {
			if (!hasFocus) return;
			Control focusControl = getDisplay().getFocusControl();
			if (focusControl is arrow || focusControl is list || focusControl is text) return;
			hasFocus = false;
			Shell shell = getShell ();
			shell.removeListener(DWT.Deactivate, listener);
			Display display = getDisplay();
			display.removeFilter(DWT.FocusIn, filter);
			Event e = new Event();
			notifyListeners(DWT.FocusOut, e);
			break;
		}
		default : break;
	}
}
/**
* Gets the index of an item.
* <p>
* The list is searched starting at 0 until an
* item is found that is equal to the search item.
* If no item is found, -1 is returned.  Indexing
* is zero based.
*
* @param string the search item
* @return the index of the item
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when string is null
*/
public int indexOf (char[] string) {
	checkWidget();
	if (string is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	return list.indexOf(string);
}
/**
* Gets the index of an item.
* <p>
* The widget is searched starting at start including
* the end position until an item is found that
* is equal to the search itenm.  If no item is
* found, -1 is returned.  Indexing is zero based.
*
* @param string the search item
* @param start the starting position
* @return the index of the item
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when string is null
*/
public int indexOf (char[] string, int start) {
	checkWidget();
	if (string is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	return list.indexOf(string, start);
}

version(OLE_COM){
void initAccessible() {
	class AA1 : AccessibleAdapter {
		CCombo c;
		public this(CCombo cc) { c=cc;}
		public void getName(AccessibleEvent e) {
			char[] name = null;
			Label label = c.getAssociatedLabel ();
			if (label !is null) {
				name = c.stripMnemonic(label.getText());
			}
			e.result = name;
		}
		public void getKeyboardShortcut(AccessibleEvent e) {
			char[] shortcut = null;
			Label label = c.getAssociatedLabel ();
			if (label !is null) {
				char[] text = label.getText();
				if (text !is null) {
					char mnemonic = c.getMnemonic(text);
					if (mnemonic != '\0') {
						shortcut = ("Alt+");
						shortcut ~= mnemonic; //$NON-NLS-1$
					}
				}
			}
			e.result = shortcut;
		}
		public void getHelp(AccessibleEvent e) {
			e.result = c.getToolTipText();
		}
	}

	AccessibleAdapter accessibleAdapter = new AA1(this) ;
	getAccessible().addAccessibleListener(accessibleAdapter);
	list.getAccessible().addAccessibleListener(accessibleAdapter);	
	text.getAccessible().addAccessibleListener(accessibleAdapter);

	class ATA1 : AccessibleTextAdapter {
		Text t;
		public this(Text txt) { t = txt;}
		public void getCaretOffset(AccessibleTextEvent e) {
			e.offset = t.getCaretPosition();
		}
	}
	getAccessible().addAccessibleTextListener(new ATA1(text));

	class ACA1 : AccessibleControlAdapter {
		CCombo c;
		public this(CCombo cc) { c=cc;}
		public void getChildAtPoint(AccessibleControlEvent e) {
			Point testPoint = c.toControl(new Point(e.x, e.y));
			if (c.getBounds().contains(testPoint)) {
				e.childID = ACC.CHILDID_SELF;
			}
		}
		
		public void getLocation(AccessibleControlEvent e) {
			Rectangle location = c.getBounds();
			Point pt = c.toDisplay(new Point(location.x, location.y));
			e.x = pt.x;
			e.y = pt.y;
			e.width = location.width;
			e.height = location.height;
		}
		
		public void getChildCount(AccessibleControlEvent e) {
			e.detail = 0;
		}
		
		public void getRole(AccessibleControlEvent e) {
			e.detail = ACC.ROLE_COMBOBOX;
		}
		
		public void getState(AccessibleControlEvent e) {
			e.detail = ACC.STATE_NORMAL;
		}

		public void getValue(AccessibleControlEvent e) {
			e.result = c.getText();
		}
	}
	getAccessible().addAccessibleControlListener(new ACA1(this) );
}
}


boolean isDropped () {
	return popup.getVisible ();
}
public boolean isFocusControl () {
	checkWidget();
	if (text.isFocusControl() || arrow.isFocusControl() || list.isFocusControl() || popup.isFocusControl()) {
		return true;
	} 
	return super.isFocusControl();
}
void internalLayout () {
	if (isDropped ()) dropDown (false);
	
	Rectangle rect = getClientArea();
	int width = rect.width;
	int height = rect.height;
	Point arrowSize = (cast(Control)arrow).computeSize(DWT.DEFAULT, height);
	(cast(Control)text).setBounds (0, 0, width - arrowSize.x, height);
	arrow.setBounds (width - arrowSize.x, 0, arrowSize.x, arrowSize.y);
}
void listEvent (Event event) {
	switch (event.type) {
		case DWT.Dispose:
			if (getShell() !is popup.getParent()) {
				char[][] items = list.getItems();
				int selectionIndex = list.getSelectionIndex();
				popup = null;
				list = null;
				createPopup(items, selectionIndex);
			}
			break;
		case DWT.FocusIn: {
			handleFocus (DWT.FocusIn);
			break;
		}
		case DWT.MouseUp: {
			if (event.button != 1) return;
			dropDown (false);
			break;
		}
		case DWT.Selection: {
			int index = list.getSelectionIndex ();
			if (index == -1) return;
			text.setText (list.getItem (index));
			text.selectAll ();
			list.setSelection(index);
			Event e = new Event();
			e.time = event.time;
			e.stateMask = event.stateMask;
			e.doit = event.doit;
			notifyListeners(DWT.Selection, e);
			event.doit = e.doit;
			break;
		}
		case DWT.Traverse: {
			switch (event.detail) {
				case DWT.TRAVERSE_RETURN:
				case DWT.TRAVERSE_ESCAPE:
				case DWT.TRAVERSE_ARROW_PREVIOUS:
				case DWT.TRAVERSE_ARROW_NEXT:
					event.doit = false;
					break;
				default : break;
			}
			Event e = new Event();
			e.time = event.time;
			e.detail = event.detail;
			e.doit = event.doit;
			e.character = event.character;
			e.keyCode = event.keyCode;
			notifyListeners(DWT.Traverse, e);
			event.doit = e.doit;
			event.detail = e.detail;
			break;
		}
		case DWT.KeyUp: {		
			Event e = new Event();
			e.time = event.time;
			e.character = event.character;
			e.keyCode = event.keyCode;
			e.stateMask = event.stateMask;
			notifyListeners(DWT.KeyUp, e);
			break;
		}
		case DWT.KeyDown: {
			if (event.character == DWT.ESC) { 
				// Escape key cancels popup list
				dropDown (false);
			}
			if ((event.stateMask & DWT.ALT) != 0 && (event.keyCode == DWT.ARROW_UP || event.keyCode == DWT.ARROW_DOWN)) {
				dropDown (false);
			}
			if (event.character == DWT.CR) {
				// Enter causes default selection
				dropDown (false);
				Event e = new Event();
				e.time = event.time;
				e.stateMask = event.stateMask;
				notifyListeners(DWT.DefaultSelection, e);
			}
			// At this point the widget may have been disposed.
			// If so, do not continue.
			if (isDisposed()) break;
			Event e = new Event();
			e.time = event.time;
			e.character = event.character;
			e.keyCode = event.keyCode;
			e.stateMask = event.stateMask;
			notifyListeners(DWT.KeyDown, e);
			break;
			
		}
		default : break;
	}
}
void popupEvent(Event event) {
	switch (event.type) {
		case DWT.Paint:
			// draw black rectangle around list
			Rectangle listRect = list.getBounds();
			Color black = getDisplay().getSystemColor(DWT.COLOR_BLACK);
			event.gc.setForeground(black);
			event.gc.drawRectangle(0, 0, listRect.width + 1, listRect.height + 1);
			break;
		case DWT.Close:
			event.doit = false;
			dropDown (false);
			break;
		case DWT.Deactivate:
			dropDown (false);
			break;
		default : break;
	}
}
public void redraw () {
	super.redraw();
	text.redraw();
	arrow.redraw();
	if (popup.isVisible()) list.redraw();
}
public void redraw (int x, int y, int width, int height, boolean all) {
	super.redraw(x, y, width, height, true);
}

/**
* Removes an item at an index.
* <p>
* Indexing is zero based.
*
* This operation will fail when the index is out
* of range or an item could not be removed from
* the OS.
*
* @param index the index of the item
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_ITEM_NOT_REMOVED)
*	when the operation fails
*/
public void remove (int index) {
	checkWidget();
	list.remove (index);
}
/**
* Removes a range of items.
* <p>
* Indexing is zero based.  The range of items
* is from the start index up to and including
* the end index.
*
* This operation will fail when the index is out
* of range or an item could not be removed from
* the OS.
*
* @param start the start of the range
* @param end the end of the range
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_ITEM_NOT_REMOVED)
*	when the operation fails
*/
public void remove (int start, int end) {
	checkWidget();
	list.remove (start, end);
}
/**
* Removes an item.
* <p>
* This operation will fail when the item
* could not be removed from the OS.
*
* @param string the search item
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when string is null
* @exception SWTError(ERROR_ITEM_NOT_REMOVED)
*	when the operation fails
*/
public void remove (char[] string) {
	checkWidget();
	if (string is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	list.remove (string);
}
/**
* Removes all items.
* <p>
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public void removeAll () {
	checkWidget();
	text.setText (""); //$NON-NLS-1$
	list.removeAll ();
}
/**	 
* Removes the listener.
* <p>
*
* @param listener the listener
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when listener is null
*/
public void removeModifyListener (ModifyListener listener) {
	checkWidget();
	if (listener is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	removeListener(DWT.Modify, listener);	
}
/**	 
* Removes the listener.
* <p>
*
* @param listener the listener
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when listener is null
*/
public void removeSelectionListener (SelectionListener listener) {
	checkWidget();
	if (listener is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	removeListener(DWT.Selection, listener);
	removeListener(DWT.DefaultSelection,listener);	
}
/**
* Selects an item.
* <p>
* If the item at an index is not selected, it is
* selected. Indices that are out of
* range are ignored.  Indexing is zero based.
*
* @param index the index of the item
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public void select (int index) {
	checkWidget();
	if (index == -1) {
		list.deselectAll ();
		text.setText (""); //$NON-NLS-1$
		return;
	}
	if (0 <= index && index < list.getItemCount()) {
		if (index != getSelectionIndex()) {
			text.setText (list.getItem (index));
			text.selectAll ();
			list.select (index);
			list.showSelection ();
		}
	}
}
public void setBackground (Color color) {
	super.setBackground(color);
	background = color;
	if (text !is null) text.setBackground(color);
	if (list !is null) list.setBackground(color);
	if (arrow !is null) arrow.setBackground(color);
}
/**
 * Sets the editable state.
 *
 * @param editable the new editable state
 *
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 *
 * @since 3.0
 *
 */
public void setEditable (boolean editable) {
	checkWidget ();
	text.setEditable(editable);
}
public void setEnabled (boolean enabled) {
	super.setEnabled(enabled);
	if (popup !is null) popup.setVisible (false);
	if (text !is null) text.setEnabled(enabled);
	if (arrow !is null) arrow.setEnabled(enabled);
}
public boolean setFocus () {
	checkWidget();
	return text.setFocus ();
}
public void setFont (Font font) {
	super.setFont (font);
	this.font = font;
	text.setFont (font);
	list.setFont (font);
	internalLayout ();
}
public void setForeground (Color color) {
	super.setForeground(color);
	foreground = color;
	if (text !is null) text.setForeground(color);
	if (list !is null) list.setForeground(color);
	if (arrow !is null) arrow.setForeground(color);
}
/**
* Sets the text of an item; indexing is zero based.
*
* This operation will fail when the index is out
* of range or an item could not be changed in
* the OS.
*
* @param index the index for the item
* @param string the item
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when items is null
* @exception SWTError(ERROR_ITEM_NOT_MODIFIED)
*	when the operation fails
*/
public void setItem (int index, char[] string) {
	checkWidget();
	if (string is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	list.setItem (index, string);
}
/**
* Sets all items.
*
* @param items the array of items
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when items is null
* @exception SWTError(ERROR_ITEM_NOT_ADDED)
*	when the operation fails
*/
public void setItems (char[][] items) {
	checkWidget();
	if (items is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	if (!text.getEditable()) text.setText (""); //$NON-NLS-1$
	list.setItems (items);
}
/**
* Sets the new selection.
*
* @param selection point representing the start and the end of the new selection
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when selection is null
*/
public void setSelection (Point selection) {
	checkWidget();
	if (selection is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	text.setSelection (selection.x, selection.y);
}

/**
* Sets the widget text.
*
* @param string the widget text
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when string is null
*/
public void setText (char[] string) {
	checkWidget();
	if (string is null) string = "";
	//DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	int index = list.indexOf(string);
	if (index == -1) {
		list.deselectAll ();
		text.setText (string);
		return;
	}
	text.setText (string);
	text.selectAll ();
	list.setSelection (index);
	list.showSelection ();
}
/**
* Sets the text limit.
* 
* @param limit new text limit
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_CANNOT_BE_ZERO)
*	when limit is 0
*/
public void setTextLimit (int limit) {
	checkWidget();
	text.setTextLimit (limit);
}

public void setToolTipText (char[] string) {
	checkWidget();
	super.setToolTipText(string);
	arrow.setToolTipText (string);
	text.setToolTipText (string);		
}

public void setVisible (boolean visible) {
	super.setVisible(visible);
	if (!visible) popup.setVisible(false);
}
/**
 * Sets the number of items that are visible in the drop
 * down portion of the receiver's list.
 *
 * @param count the new number of items to be visible
 *
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 * 
 * @since 3.0
 */
public void setVisibleItemCount (int count) {
	checkWidget ();
	if (count < 0) return;
	visibleItemCount = count;
}

char[] stripMnemonic (char[] string) {
	int index = 0;
	int len = string.length;
	do {
		while ((index < len) && (Converter.charAt(string, index) != '&')) index++;
		if (++index >= len) return string;
		if (Converter.charAt(string, index) != '&') {
			return Converter.substring(string, 0, index-1) ~ Converter.substring(string, index, len);
		}
		index++;
	} while (index < len);
 	return string;
}
void textEvent (Event event) {
	switch (event.type) {
		case DWT.FocusIn: {
			handleFocus (DWT.FocusIn);
			break;
		}
		case DWT.KeyDown: {
			if (event.character == DWT.CR) {
				dropDown (false);
				Event e = new Event();
				e.time = event.time;
				e.stateMask = event.stateMask;
				notifyListeners(DWT.DefaultSelection, e);
			}
			//At this point the widget may have been disposed.
			// If so, do not continue.
			if (isDisposed()) break;
			
			if (event.keyCode == DWT.ARROW_UP || event.keyCode == DWT.ARROW_DOWN) {
				if ((event.stateMask & DWT.ALT) != 0) {
					boolean dropped = isDropped ();
					text.selectAll ();
					if (!dropped) setFocus ();
					dropDown (cast(boolean)!dropped);
					break;
				}

				int oldIndex = getSelectionIndex ();
				if (event.keyCode == DWT.ARROW_UP) {
					select (Math.max (oldIndex - 1, 0));
				} else {
					select (Math.min (oldIndex + 1, getItemCount () - 1));
				}
		
				if (oldIndex != getSelectionIndex ()) {
					Event e = new Event();
					e.time = event.time;
					e.stateMask = event.stateMask;
					notifyListeners(DWT.Selection, e);
				}
				//At this point the widget may have been disposed.
				// If so, do not continue.
				if (isDisposed()) break;
			}
			
			// Further work : Need to add support for incremental search in 
			// pop up list as characters typed in text widget
						
			Event e = new Event();
			e.time = event.time;
			e.character = event.character;
			e.keyCode = event.keyCode;
			e.stateMask = event.stateMask;
			notifyListeners(DWT.KeyDown, e);
			break;
		}
		case DWT.KeyUp: {
			Event e = new Event();
			e.time = event.time;
			e.character = event.character;
			e.keyCode = event.keyCode;
			e.stateMask = event.stateMask;
			notifyListeners(DWT.KeyUp, e);
			break;
		}
		case DWT.Modify: {
			list.deselectAll ();
			Event e = new Event();
			e.time = event.time;
			notifyListeners(DWT.Modify, e);
			break;
		}
		case DWT.MouseDown: {
			if (event.button != 1) return;
			if (text.getEditable ()) return;
			boolean dropped = isDropped ();
			text.selectAll ();
			if (!dropped) setFocus ();
			dropDown (cast(boolean)!dropped);
			break;
		}
		case DWT.MouseUp: {
			if (event.button != 1) return;
			if (text.getEditable ()) return;
			text.selectAll ();
			break;
		}
		case DWT.Traverse: {		
			switch (event.detail) {
				case DWT.TRAVERSE_RETURN:
				case DWT.TRAVERSE_ARROW_PREVIOUS:
				case DWT.TRAVERSE_ARROW_NEXT:
					// The enter causes default selection and
					// the arrow keys are used to manipulate the list contents so
					// do not use them for traversal.
					event.doit = false;
					break;
				default : break;
			}
			
			Event e = new Event();
			e.time = event.time;
			e.detail = event.detail;
			e.doit = event.doit;
			e.character = event.character;
			e.keyCode = event.keyCode;
			notifyListeners(DWT.Traverse, e);
			event.doit = e.doit;
			event.detail = e.detail;
			break;
		}
		default : break;
	}
}


// Event Handler
public void handleModify(Object customData, void delegate(ModifyEvent) func){
	handleEvent(new ModifyHandler(customData, DWT.Modify, func));
}
public void handleSelection(Object customData, void delegate(SelectionEvent) func){
	handleEvent(new SelectionHandler(customData, DWT.Selection, func));
}
public void handleDefaultSelection(Object customData, void delegate(SelectionEvent) func ){
	handleEvent(new SelectionHandler(customData, DWT.DefaultSelection, func));
}


}
